package com.weirdor.boot.module.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weirdor.boot.common.utils.PageUtils;
import com.weirdor.boot.common.utils.Query;
import com.weirdor.boot.module.entity.SysUser;
import com.weirdor.boot.module.mapper.SysUserMapper;
import com.weirdor.boot.module.service.ISysUserRoleService;
import com.weirdor.boot.module.service.ISysUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.weirdor.boot.module.utils.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2018-03-16
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return baseMapper.queryAllMenuId(userId);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String username = (String)params.get("username");

        Page<SysUser> page = this.selectPage(
                new Query<SysUser>(params).getPage(),
                new EntityWrapper<SysUser>()
                        .like(StringUtils.isNotBlank(username),"username", username)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysUser user) {
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setSalt(salt);
        user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        this.insert(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysUser user) {
        if(StringUtils.isBlank(user.getPassword())){
            user.setPassword(null);
        }else{
            user.setPassword(ShiroUtils.sha256(user.getPassword(), user.getSalt()));
        }
        this.updateById(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getUserId(), user.getRoleIdList());
    }


    @Override
    public boolean updatePassword(Long userId, String password, String newPassword) {
        SysUser userEntity = new SysUser();
        userEntity.setPassword(newPassword);
        return this.update(userEntity,
                new EntityWrapper<SysUser>().eq("user_id", userId).eq("password", password));
    }

    @Override
    public List<String> queryAllPerms(long userId) {
        return baseMapper.queryAllPerms(userId);
    }

    @Override
    public SysUser queryByUserName(String username) {
        EntityWrapper<SysUser> ew=new EntityWrapper<>();
        ew.eq("username",username);
        return super.selectOne(ew);
    }

}
